layui.use(['table', 'admin', 'form', 'ax','layer'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;
    var layer = layui.layer;

    var phTable = {
        tableId: "phTable"
    };

    var queryData = {};
    queryData['shopCode'] = Feng.getUrlParam("shopCode");
    queryData['shopName'] = '';
    queryData['beginTime'] = Feng.getUrlParam("beginTime");
    queryData['endTime'] = Feng.getUrlParam("endTime");

    phTable.initColumn = function () {
        return  [[
            {field: 'num', width: 100, title: '', align: "center"},
            {field: 'shopCode', width: 90, title: '门店编号', align: "center"},
            {field: 'shopName', width: 220, title: '门店名称', templet:'#shopName',align: "center"},
            {field: 'scmClassCode', width: 110, title: '品相类别编号',align: "center"},
            {field: 'className', width: 120, title: '品相类别名称', align: "center",},
            {field: 'proCode', width: 110, title: '品相编号', align: "center"},
            {field: 'proName', width: 130, title: '品相名称', align: "center",},
            {field: 'spec', width: 120, title: '规格', align: "center"},
            {field: 'unit', width: 90, title: '单位', align: "center",},
            {field: 'price', width: 100, title: '非税成本单价', align: "center"},
            {field: 'thNum', width: 90, title: '销售数量', align: "center",},
            {field: 'realNum', width: 110, title: '实际耗用数量', align: "center",},
            {field: 'numTotal', width: 120, title: '耗用数量分差', align: "center"},
            {field: 'amountTotal', width: 120, title: '耗用金额分差', align: "center"},
        ]]
    };
    var tableResult= table.render({
        elem: '#phTable',
        url: Feng.ctxPath + '/btyCwHuopei/huopeiInfo',
        where: queryData,
        page: false,
        cellMinWidth: 150,//全局定义常规单元格的最小宽度，layui 2.2.1 新增
        cols:phTable.initColumn(),
        done:function (res, curr, count,) {
            var that = this.elem.next();
            res.data.forEach(function (item, index) {
                if (item.shopName === "类别名称") {
                    var tr = that.find(".layui-table-box tbody tr[data-index='" + index + "']");
                    var td1 = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='shopCode']");
                    tr.css("background-color", "#e1e2e6");
                    tr.css("color", "#666666");
                    td1.css("color", "#e1e2e6");
                }
                if (item.shopName === "清江鱼") {
                    var tr = that.find(".layui-table-box tbody tr[data-index='" + index + "']");
                    var td1 = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='shopCode']");
                    tr.css("background-color", "#e1e2e6");
                    tr.css("color", "#666666");
                    td1.css("color", "#e1e2e6");
                }
                if (item.shopName === "清江鱼") {
                    var td = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='proCode']");
                    td.attr("data-edit", "text");
                    td.css("background-color", "rgb(113 203 242)");
                }
                if (item.shopName === "清江鱼") {
                    var td = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='proName']");
                    td.attr("data-edit", "text");
                    td.css("background-color", "rgb(113 203 242)");
                }
                if (item.shopName === "清江鱼") {
                    var td = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='price']");
                    td.attr("data-edit", "text");
                    td.css("background-color", "rgb(113 203 242)");
                }
                if (item.shopName === "酱料") {
                    var tr = that.find(".layui-table-box tbody tr[data-index='" + index + "']");
                    var td1 = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='shopCode']");
                    tr.css("background-color", "#e1e2e6");
                    tr.css("color", "#666666");
                    td1.css("color", "#e1e2e6");
                }
                if (item.shopName === "酱料") {
                    var td = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='proCode']");
                    td.attr("data-edit", "text");
                    td.css("background-color", "rgb(113 203 242)");
                }
                if (item.shopName === "酱料") {
                    var td = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='proName']");
                    td.attr("data-edit", "text");
                    td.css("background-color", "rgb(113 203 242)");
                }
                if (item.shopName === "酱料") {
                    var td = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='price']");
                    td.attr("data-edit", "text");
                    td.css("background-color", "rgb(113 203 242)");
                }
                if (item.shopName === "酒水") {
                    var tr = that.find(".layui-table-box tbody tr[data-index='" + index + "']");
                    var td1 = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='shopCode']");
                    tr.css("background-color", "#e1e2e6");
                    tr.css("color", "#666666");
                    td1.css("color", "#e1e2e6");
                }
                if (item.shopName === "酒水") {
                    var td = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='proCode']");
                    td.attr("data-edit", "text");
                    td.css("background-color", "rgb(113 203 242)");
                }
                if (item.shopName === "酒水") {
                    var td = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='proName']");
                    td.attr("data-edit", "text");
                    td.css("background-color", "rgb(113 203 242)");
                }
                if (item.shopName === "酒水") {
                    var td = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='price']");
                    td.attr("data-edit", "text");
                    td.css("background-color", "rgb(113 203 242)");
                }
                // //酒水卖价可修改
                // if ((item.className === "酒水" || item.className === "自采酒水") && item.num != "小计") {
                //     var td = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='price']");
                //     td.attr("data-edit", "text");
                // }
                if (item.num === "小计") {
                    var td = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='thNum']").css("background-color", "#a6d792");
                    var td = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='realNum']").css("background-color", "#a6d792");
                    var td = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='numTotal']").css("background-color", "#a6d792");
                    var td = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='amountTotal']").css("background-color", "#a6d792");
                }
                // if (item.num === "总计") {
                //     var td = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='soup']").removeAttr("data-edit", "text");
                //     var td = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='soupAmount']").removeAttr("data-edit", "text");
                //     var td = that.find(".layui-table-box tbody tr[data-index='" + index + "'] td[data-field='thNum']").removeAttr("data-edit", "text");
                // }
            });

            //合并单元格列
            let fix = $('td[data-field="thNum"]');
            fix.eq(fix.length - 4).attr('rowspan', 3);
            fix.eq(fix.length - 3).remove();
            fix.eq(fix.length - 2).remove();
            fix.eq(fix.length - 1).remove();
            fix.eq(fix.length).remove()
        }
    });

    //返回按钮
    $("#backupPage").click(function () {
        window.location.href = Feng.ctxPath + '/btyCwHuopei'
    });

    // 导出excel
    $('#btnExp').click(function () {
        var shopCode = Feng.getUrlParam("shopCode");
        var beginTime = Feng.getUrlParam("beginTime");
        var endTime = Feng.getUrlParam("endTime");
        if (shopCode == "" || shopCode == undefined || shopCode == null) {
            Feng.error("请先选择门店");
        } else {
            //获取导出Excel的数据
            // var url = Feng.ctxPath + "/btyCwHuopei/listExport?shopCode=" + shopCode + "&beginTime=" + beginTime + "&endTime=" + endTime;
            var url = Feng.ctxPath + "/btyCwHuopei/exportHp?shopCode=" + shopCode + "&beginTime=" + beginTime + "&endTime=" + endTime;
            location.href = url;
        }
    });

    //监听单元格编辑
    table.on('edit(phTable)', function(obj){
        //得到修改后的值
        var value = obj.value //得到修改后的值
            ,data = obj.data //得到所在行所有键值
            ,field = obj.field; //得到字段

        $.ajax({
            type: "PUT",
            url: "/btyCwHuopei/editHuopei",
            datatype: "json",
            data: data,
            success: function (data) {
                table.reload(phTable.tableId);
            }, error: function () {
                // Feng.error("修改失败！");
            }, complete: function (XHR, TS) { }
        });
    });
});
